package org.dromara.yuanshidian.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.yuanshidian.domain.bo.Ab03Bo;
import org.dromara.yuanshidian.domain.vo.Ab03Vo;
import org.dromara.yuanshidian.domain.Ab03;
import org.dromara.yuanshidian.mapper.Ab03Mapper;
import org.dromara.yuanshidian.service.IAb03Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 原离退人员信息Service业务层处理
 *
 * @author TzhongTop
 * @date 2025-01-24
 */
@RequiredArgsConstructor
@Service
public class Ab03ServiceImpl implements IAb03Service {

    private final Ab03Mapper baseMapper;

    /**
     * 查询原离退人员信息
     *
     * @param id 主键
     * @return 原离退人员信息
     */
    @Override
    public Ab03Vo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询原离退人员信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 原离退人员信息分页列表
     */
    @Override
    public TableDataInfo<Ab03Vo> queryPageList(Ab03Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Ab03> lqw = buildQueryWrapper(bo);
        Page<Ab03Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的原离退人员信息列表
     *
     * @param bo 查询条件
     * @return 原离退人员信息列表
     */
    @Override
    public List<Ab03Vo> queryList(Ab03Bo bo) {
        LambdaQueryWrapper<Ab03> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Ab03> buildQueryWrapper(Ab03Bo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Ab03> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAaa027()), Ab03::getAaa027, bo.getAaa027());
        lqw.eq(bo.getGr00() != null, Ab03::getGr00, bo.getGr00());
        lqw.eq(bo.getGrid() != null, Ab03::getGrid, bo.getGrid());
        lqw.eq(StringUtils.isNotBlank(bo.getDwh()), Ab03::getDwh, bo.getDwh());
        lqw.eq(StringUtils.isNotBlank(bo.getGrbh()), Ab03::getGrbh, bo.getGrbh());
        lqw.eq(StringUtils.isNotBlank(bo.getTx01()), Ab03::getTx01, bo.getTx01());
        lqw.eq(StringUtils.isNotBlank(bo.getDw01()), Ab03::getDw01, bo.getDw01());
        lqw.eq(StringUtils.isNotBlank(bo.getDw02()), Ab03::getDw02, bo.getDw02());
        lqw.eq(StringUtils.isNotBlank(bo.getGr02()), Ab03::getGr02, bo.getGr02());
        lqw.eq(StringUtils.isNotBlank(bo.getGr03()), Ab03::getGr03, bo.getGr03());
        lqw.eq(StringUtils.isNotBlank(bo.getGr04()), Ab03::getGr04, bo.getGr04());
        lqw.eq(StringUtils.isNotBlank(bo.getGr05()), Ab03::getGr05, bo.getGr05());
        lqw.eq(StringUtils.isNotBlank(bo.getGr06()), Ab03::getGr06, bo.getGr06());
        lqw.eq(bo.getGr07() != null, Ab03::getGr07, bo.getGr07());
        lqw.eq(bo.getGr08() != null, Ab03::getGr08, bo.getGr08());
        lqw.eq(StringUtils.isNotBlank(bo.getGr09()), Ab03::getGr09, bo.getGr09());
        lqw.eq(bo.getGr10() != null, Ab03::getGr10, bo.getGr10());
        lqw.eq(bo.getGr11() != null, Ab03::getGr11, bo.getGr11());
        lqw.eq(bo.getGr12() != null, Ab03::getGr12, bo.getGr12());
        lqw.eq(bo.getGr13() != null, Ab03::getGr13, bo.getGr13());
        lqw.eq(bo.getGr14() != null, Ab03::getGr14, bo.getGr14());
        lqw.eq(bo.getGr15() != null, Ab03::getGr15, bo.getGr15());
        lqw.eq(bo.getGr16() != null, Ab03::getGr16, bo.getGr16());
        lqw.eq(bo.getGr17() != null, Ab03::getGr17, bo.getGr17());
        lqw.eq(bo.getGr18() != null, Ab03::getGr18, bo.getGr18());
        lqw.eq(bo.getGr19() != null, Ab03::getGr19, bo.getGr19());
        lqw.eq(bo.getGr20() != null, Ab03::getGr20, bo.getGr20());
        lqw.eq(bo.getGr21() != null, Ab03::getGr21, bo.getGr21());
        lqw.eq(bo.getGr60() != null, Ab03::getGr60, bo.getGr60());
        lqw.eq(bo.getTx02() != null, Ab03::getTx02, bo.getTx02());
        lqw.eq(StringUtils.isNotBlank(bo.getTx03()), Ab03::getTx03, bo.getTx03());
        lqw.eq(bo.getTx04() != null, Ab03::getTx04, bo.getTx04());
        lqw.eq(bo.getTx05() != null, Ab03::getTx05, bo.getTx05());
        lqw.eq(bo.getTx06() != null, Ab03::getTx06, bo.getTx06());
        lqw.eq(bo.getTx07() != null, Ab03::getTx07, bo.getTx07());
        lqw.eq(bo.getTx08() != null, Ab03::getTx08, bo.getTx08());
        lqw.eq(bo.getTx09() != null, Ab03::getTx09, bo.getTx09());
        lqw.eq(bo.getTx10() != null, Ab03::getTx10, bo.getTx10());
        lqw.eq(bo.getTx11() != null, Ab03::getTx11, bo.getTx11());
        lqw.eq(bo.getTx110() != null, Ab03::getTx110, bo.getTx110());
        lqw.eq(bo.getTx12() != null, Ab03::getTx12, bo.getTx12());
        lqw.eq(bo.getTx13() != null, Ab03::getTx13, bo.getTx13());
        lqw.eq(bo.getTx14() != null, Ab03::getTx14, bo.getTx14());
        lqw.eq(bo.getTx15() != null, Ab03::getTx15, bo.getTx15());
        lqw.eq(bo.getTx16() != null, Ab03::getTx16, bo.getTx16());
        lqw.eq(StringUtils.isNotBlank(bo.getTx17()), Ab03::getTx17, bo.getTx17());
        lqw.eq(StringUtils.isNotBlank(bo.getTx18()), Ab03::getTx18, bo.getTx18());
        lqw.eq(bo.getTx19() != null, Ab03::getTx19, bo.getTx19());
        lqw.eq(bo.getTx24() != null, Ab03::getTx24, bo.getTx24());
        lqw.eq(StringUtils.isNotBlank(bo.getTx25()), Ab03::getTx25, bo.getTx25());
        lqw.eq(bo.getTx26() != null, Ab03::getTx26, bo.getTx26());
        lqw.eq(StringUtils.isNotBlank(bo.getTx27()), Ab03::getTx27, bo.getTx27());
        lqw.eq(bo.getTx28() != null, Ab03::getTx28, bo.getTx28());
        lqw.eq(bo.getTx29() != null, Ab03::getTx29, bo.getTx29());
        lqw.eq(bo.getTx30() != null, Ab03::getTx30, bo.getTx30());
        lqw.eq(bo.getTx31() != null, Ab03::getTx31, bo.getTx31());
        lqw.eq(bo.getTx32() != null, Ab03::getTx32, bo.getTx32());
        lqw.eq(bo.getTx33() != null, Ab03::getTx33, bo.getTx33());
        lqw.eq(bo.getTx34() != null, Ab03::getTx34, bo.getTx34());
        lqw.eq(bo.getTx35() != null, Ab03::getTx35, bo.getTx35());
        lqw.eq(bo.getTx40() != null, Ab03::getTx40, bo.getTx40());
        lqw.eq(bo.getTx41() != null, Ab03::getTx41, bo.getTx41());
        lqw.eq(bo.getTx42() != null, Ab03::getTx42, bo.getTx42());
        lqw.eq(bo.getTx43() != null, Ab03::getTx43, bo.getTx43());
        lqw.eq(bo.getTx44() != null, Ab03::getTx44, bo.getTx44());
        lqw.eq(bo.getTx50() != null, Ab03::getTx50, bo.getTx50());
        lqw.eq(bo.getTx51() != null, Ab03::getTx51, bo.getTx51());
        lqw.eq(StringUtils.isNotBlank(bo.getTx52()), Ab03::getTx52, bo.getTx52());
        lqw.eq(bo.getTx53() != null, Ab03::getTx53, bo.getTx53());
        lqw.eq(StringUtils.isNotBlank(bo.getTx54()), Ab03::getTx54, bo.getTx54());
        lqw.eq(StringUtils.isNotBlank(bo.getYd03()), Ab03::getYd03, bo.getYd03());
        lqw.eq(StringUtils.isNotBlank(bo.getYd08()), Ab03::getYd08, bo.getYd08());
        lqw.eq(bo.getYd10() != null, Ab03::getYd10, bo.getYd10());
        lqw.eq(StringUtils.isNotBlank(bo.getYd11()), Ab03::getYd11, bo.getYd11());
        lqw.eq(bo.getCs39() != null, Ab03::getCs39, bo.getCs39());
        lqw.eq(StringUtils.isNotBlank(bo.getDw19()), Ab03::getDw19, bo.getDw19());
        lqw.eq(StringUtils.isNotBlank(bo.getEd()), Ab03::getEd, bo.getEd());
        lqw.eq(bo.getEdsj() != null, Ab03::getEdsj, bo.getEdsj());
        lqw.eq(bo.getLtssj() != null, Ab03::getLtssj, bo.getLtssj());
        lqw.eq(StringUtils.isNotBlank(bo.getDw24()), Ab03::getDw24, bo.getDw24());
        lqw.eq(bo.getSystime() != null, Ab03::getSystime, bo.getSystime());
        lqw.eq(StringUtils.isNotBlank(bo.getTx56()), Ab03::getTx56, bo.getTx56());
        lqw.eq(StringUtils.isNotBlank(bo.getTx57()), Ab03::getTx57, bo.getTx57());
        lqw.eq(StringUtils.isNotBlank(bo.getTx58()), Ab03::getTx58, bo.getTx58());
        lqw.eq(StringUtils.isNotBlank(bo.getTx59()), Ab03::getTx59, bo.getTx59());
        lqw.eq(StringUtils.isNotBlank(bo.getTx60()), Ab03::getTx60, bo.getTx60());
        lqw.eq(StringUtils.isNotBlank(bo.getTx61()), Ab03::getTx61, bo.getTx61());
        lqw.eq(StringUtils.isNotBlank(bo.getTx62()), Ab03::getTx62, bo.getTx62());
        lqw.eq(StringUtils.isNotBlank(bo.getTx63()), Ab03::getTx63, bo.getTx63());
        lqw.eq(StringUtils.isNotBlank(bo.getTx64()), Ab03::getTx64, bo.getTx64());
        lqw.eq(StringUtils.isNotBlank(bo.getTx65()), Ab03::getTx65, bo.getTx65());
        lqw.eq(StringUtils.isNotBlank(bo.getPass()), Ab03::getPass, bo.getPass());
        lqw.eq(StringUtils.isNotBlank(bo.getFfbj()), Ab03::getFfbj, bo.getFfbj());
        lqw.eq(StringUtils.isNotBlank(bo.getTx27A()), Ab03::getTx27A, bo.getTx27A());
        lqw.eq(StringUtils.isNotBlank(bo.getTx27B()), Ab03::getTx27B, bo.getTx27B());
        lqw.eq(StringUtils.isNotBlank(bo.getDw24A()), Ab03::getDw24A, bo.getDw24A());
        lqw.eq(StringUtils.isNotBlank(bo.getDw24B()), Ab03::getDw24B, bo.getDw24B());
        lqw.eq(bo.getTx67() != null, Ab03::getTx67, bo.getTx67());
        lqw.eq(bo.getTx68() != null, Ab03::getTx68, bo.getTx68());
        lqw.eq(bo.getTx69() != null, Ab03::getTx69, bo.getTx69());
        lqw.eq(bo.getTx70() != null, Ab03::getTx70, bo.getTx70());
        lqw.eq(bo.getTx71() != null, Ab03::getTx71, bo.getTx71());
        lqw.eq(StringUtils.isNotBlank(bo.getTx72()), Ab03::getTx72, bo.getTx72());
        lqw.eq(StringUtils.isNotBlank(bo.getTqtx01()), Ab03::getTqtx01, bo.getTqtx01());
        lqw.eq(StringUtils.isNotBlank(bo.getTqtx02()), Ab03::getTqtx02, bo.getTqtx02());
        lqw.eq(StringUtils.isNotBlank(bo.getTqtx03()), Ab03::getTqtx03, bo.getTqtx03());
        lqw.eq(bo.getTqtx04() != null, Ab03::getTqtx04, bo.getTqtx04());
        lqw.eq(bo.getTqtx05() != null, Ab03::getTqtx05, bo.getTqtx05());
        lqw.eq(bo.getTqtx16() != null, Ab03::getTqtx16, bo.getTqtx16());
        lqw.eq(bo.getTx55() != null, Ab03::getTx55, bo.getTx55());
        lqw.eq(bo.getTx66() != null, Ab03::getTx66, bo.getTx66());
        lqw.eq(bo.getBgylf() != null, Ab03::getBgylf, bo.getBgylf());
        lqw.eq(bo.getFtcfy() != null, Ab03::getFtcfy, bo.getFtcfy());
        lqw.eq(StringUtils.isNotBlank(bo.getGr25()), Ab03::getGr25, bo.getGr25());
        lqw.eq(bo.getCs34() != null, Ab03::getCs34, bo.getCs34());
        lqw.eq(bo.getQtbt() != null, Ab03::getQtbt, bo.getQtbt());
        lqw.eq(StringUtils.isNotBlank(bo.getSfbj()), Ab03::getSfbj, bo.getSfbj());
        lqw.eq(bo.getTx15a() != null, Ab03::getTx15a, bo.getTx15a());
        lqw.eq(bo.getTx20() != null, Ab03::getTx20, bo.getTx20());
        lqw.eq(bo.getTx21() != null, Ab03::getTx21, bo.getTx21());
        lqw.eq(bo.getTx22() != null, Ab03::getTx22, bo.getTx22());
        lqw.eq(bo.getTx23() != null, Ab03::getTx23, bo.getTx23());
        lqw.eq(StringUtils.isNotBlank(bo.getTx74()), Ab03::getTx74, bo.getTx74());
        lqw.eq(bo.getGr11b() != null, Ab03::getGr11b, bo.getGr11b());
        lqw.eq(StringUtils.isNotBlank(bo.getQytxbf()), Ab03::getQytxbf, bo.getQytxbf());
        lqw.eq(bo.getStatus() != null, Ab03::getStatus, bo.getStatus());
        lqw.eq(bo.getDelStatus() != null, Ab03::getDelStatus, bo.getDelStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFileurl()), Ab03::getFileurl, bo.getFileurl());
        lqw.eq(StringUtils.isNotBlank(bo.getTableflieurl()), Ab03::getTableflieurl, bo.getTableflieurl());
        return lqw;
    }

    /**
     * 新增原离退人员信息
     *
     * @param bo 原离退人员信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Ab03Bo bo) {
        Ab03 add = MapstructUtils.convert(bo, Ab03.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改原离退人员信息
     *
     * @param bo 原离退人员信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Ab03Bo bo) {
        Ab03 update = MapstructUtils.convert(bo, Ab03.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Ab03 entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除原离退人员信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
